package com.yicong.ants.utils

import android.content.Context
import android.util.Log
import com.cchao.simplelib.Const
import com.cqyc.imview.TUICallKitUtils
import com.cqyc.network.getid.GetMyInformationBox.addInterface
import com.cqyc.network.getid.OnGetMyInformation
import com.cqyc.network.http.DataCallBack
import com.cqyc.network.http.HttpUtil
import com.cqyc.network.http.NConfig.Companion.initWebLink
import com.tencent.qcloud.tim.tuiofflinepush.TUIOfflinePushConfig
import com.tencent.qcloud.tuicore.interfaces.TUILoginListener
import com.yicong.ants.manager.AppHelper
import com.yicong.ants.manager.UserManager


object InitImUtils {
    fun init(context: Context) {
        TUIOfflinePushConfig.getInstance().context = context
        TUICallKitUtils.init(context, object : TUILoginListener() {
            override fun onUserSigExpired() {
                Log.e("App", "userSing已过期")
                reLogin(context)
            }

            override fun onKickedOffline() {
                Log.e("App", "被挤下线----重新登录")
            }
        })

        //http://mall_service_api.yctourism.com //http://mall.service.api.ant.test.yctourism.com
        //http://mall_service_merchant.yctourism.com
        val url = if (AppHelper.isTestEnv()) {
            "http://mall.service.api.ant.test.yctourism.com"
        } else {
            "http://mall_service_api.yctourism.com"
        }
        HttpUtil.init(url)

        addUserListener()
        initWebLink(
            "YcWebViewActivity",
            Const.Extra.Web_View_Url,
            Const.Extra.Web_View_Tile,
            Const.Extra.Web_View_Title_Visible
        )
    }

    fun log(context: Context, callBack: TUICallKitUtils.OnImLogCallBack) {
        val userBean = UserManager.getUserBean()
        TUICallKitUtils.logo(
            context,
            userBean.id,
            AppHelper.isTestEnv(),
            userBean.user_sig,
            userBean.nickname,
            userBean.avatar,
            callBack
        )
    }

    fun reLogin(context: Context) {
        val userBean = UserManager.getUserBean()
        HttpUtil.api.getUserSig("").enqueue(DataCallBack(onResponse = {
            if (it.isNotEmpty()) {
                userBean.user_sig = it
                TUICallKitUtils.logo(
                    context,
                    userBean.id,
                    AppHelper.isTestEnv(),
                    userBean.user_sig,
                    userBean.nickname,
                    userBean.avatar,
                    null
                )
                UserManager.updateUserBean(userBean)
            }
        }))
    }

    fun addUserListener() {
        addInterface(object : OnGetMyInformation {
            override fun getSource(): String {
                return "ant"
            }

            override fun isBuyer(): Boolean {
                return true
            }

            override fun getUId(): String {
                return UserManager.getUserBean().id
            }

            override fun getVersion(): String {
                return androidx.multidex.BuildConfig.VERSION_CODE.toString()
            }

            override fun getAppId(): String {
                return androidx.multidex.BuildConfig.APPLICATION_ID
            }

            override fun getToken(): String {
                return UserManager.getUserBean().jwt_token
            }

            override fun getId(): String {
                return UserManager.getUserBean().id
            }
        })
    }
}